package org.lex.ftp.ui.queue.actions;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.border.Border;

import org.lex.ftp.FTP;
import org.lex.ftp.ui.actions.FTPAction;
import org.lex.ftp.ui.prefs.LabelRenderer;
import org.lex.ftp.ui.queue.QueueInfoTableModel;
import org.lex.swing.widgets.EnhancedDialog;


public class QueueInfoAction extends FTPAction {
	public static void main(String[] args) {
		new QueueInfoAction(null).showQueueInfoDialog();
	}

	public QueueInfoAction(FTP app) {
		super(app);
		this.setName("Queue Info...");
		this.setAcceleratorKey(KeyStroke.getKeyStroke(KeyEvent.VK_I, KeyEvent.CTRL_DOWN_MASK));
	}

	public void actionPerformed(ActionEvent e) {
		this.showQueueInfoDialog();
	}

	private void showQueueInfoDialog() {
		JDialog dialog = this.createDialog();
		dialog.setSize(300, 200);
		dialog.setLocationRelativeTo(app.getRootWindow());
		dialog.setVisible(true);
	}

	private JDialog createDialog() {
		// create dialog
		final EnhancedDialog dialog = new EnhancedDialog(app.getRootWindow());
		// create content pane
		JPanel pane = new JPanel(new BorderLayout());
		JTable table = this.createTable();
		JScrollPane scroll = new JScrollPane(table);
		pane.add(scroll, BorderLayout.CENTER);
		pane.add(Box.createVerticalStrut(12), BorderLayout.NORTH);
		pane.add(Box.createHorizontalStrut(12), BorderLayout.WEST);
		pane.add(Box.createHorizontalStrut(11), BorderLayout.EAST);
		// create control button pane
		JButton ok = new JButton("OK");
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dialog.dispose();
			}
		});
		FlowLayout layout = new FlowLayout();
		layout.setAlignment(FlowLayout.CENTER);
		layout.setHgap(11);
		layout.setVgap(11);
		JPanel buttonPane = new JPanel(layout);
		buttonPane.add(ok);
		pane.add(buttonPane, BorderLayout.SOUTH);
		// add content pane to dialog
		dialog.setContentPane(pane);
		dialog.setTitle("Queue Summary");
		dialog.setDefaultButton(ok);
		return dialog;
	}

	private JTable createTable() {
		JTable table = new JTable(new QueueInfoTableModel(app));
		table.setRowHeight(table.getRowHeight() * 3 / 2);
		LabelRenderer lr = new LabelRenderer();
		lr.setFont(table.getFont());
		table.getColumnModel().getColumn(0).setCellRenderer(lr);
		return table;
	}

	static class ScrollPaneBorder implements Border {
		public Insets getBorderInsets(Component c) {
			return new Insets(1, 1, 0, 0);
		}

		public boolean isBorderOpaque() {
			return false;
		}

		public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
			// new EtchedBorder();
			g.translate(x, y);
			g.setColor(c.getBackground().darker());
			g.drawLine(0, 0, width, 0);
			g.drawLine(0, 0, 0, height);
			g.translate(-x, -y);
		}
	}
}
